/**
 * Created with JetBrains PhpStorm.
 * User: power_yin
 * Date: 13-10-11
 * Time: 下午11:47
 * To change this template use File | Settings | File Templates.
 */
(function(xf, $){
    XF.CustomerServiceList = (function(){
        /* 类静态变量 */
        var ANIMATE_TYPE_EX = 'swing',
            ANIMATE_TYPE_RE = 'swing',
            INTERVAL = 500,
            CHANGE_LENGTH = 0,
            expandedCallFun  = new Function(),
            recoverCallFn = new Function(),
            FIRST_CHILD = null,
            LAST_CHILD = null,
            COUNTER = 0;
        function CustomerServiceList(setting){
            this.wrapperSelector = setting.wrapperSelector;
            this.expandAnimateType = setting.expandnAnimateType;
            this.recoverAnimateType = setting.recoverAnimateType;
            this.interval = setting.interval;
            this.changeLength = setting.changeLength;

            //callback
            //this.expandedCallFun = setting.expandedFn;
            //this.recoverCallFn = setting.recoverFn;

            this.listUl = null;
            this.listAllLi = null;

            this._init();
        }
        CustomerServiceList.prototype._init = function(){
            this.listUl = $(this.wrapperSelector + '>ul.customer-service-list');
            this.listAllLi = this.listUl.children('li');

            FIRST_CHILD = this.listAllLi.first();
            LAST_CHILD = this.listAllLi.last();

            this.listAllLi.each(function(index, li){
                $(li).css({'z-index' : 1000 - index});
            });

            ANIMATE_TYPE_EX = this.expandAnimateType;
            ANIMATE_TYPE_RE = this.recoverAnimateType;

            INTERVAL = this.interval;
            CHANGE_LENGTH = this.changeLength;
            //expandedCallFun = this.expandedCallFun;
            //recoverCallFn = this.recoverCallFn;
        };
        CustomerServiceList.prototype.expand = function(callback){
            expandedCallFun = callback;
            this.listAllLi.first().animate({
                top : '0px',
                opacity : 1.0
            }, this.interval, ANIMATE_TYPE_EX, _nextMover);
        };
        CustomerServiceList.prototype.recoverAll = function(callback){
            recoverCallFn = callback;
            this.listAllLi.last().animate({
                top : '0px',
                opacity : 0.0
            }, this.interval, ANIMATE_TYPE_RE, _nextMover_recover);
        };
        var _nextMover = function( ){
            var nextNode = $(this).next();
            COUNTER ++;
            if(nextNode.length == 0){
                expandedCallFun.call(this);
                COUNTER = 0;
            }
            nextNode.animate({
                top : COUNTER * CHANGE_LENGTH + 'px',
                opacity : 1.0
            }, INTERVAL, ANIMATE_TYPE_EX, _nextMover);
        };
        var _nextMover_recover = function(){
            var prevNode = $(this).prev();
            if(prevNode.length == 0){
                recoverCallFn.call(this);
            }
            if(FIRST_CHILD[0] != prevNode[0]){
                prevNode.animate({
                    top : '0px',
                    opacity : 0.0
                }, INTERVAL, ANIMATE_TYPE_RE, _nextMover_recover);
            }else{
                recoverCallFn.call(this);
            }
        };

        return CustomerServiceList;
    })();

})(XF || (XF = {}), jQuery );